<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AdminServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //\Encore\Admin\Facades\Admin::disablePjax();
        $this->initGrid();
        $this->initForm();
        $this->initShow();

        $this->style();
        $this->script();
        $this->js();
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }


    protected function initGrid()
    {
        \Encore\Admin\Grid\Column::extend('layerShow', \App\Extensions\Column\LayerShow::class);
        \Encore\Admin\Grid\Column::extend('download', \App\Extensions\Column\Download::class);
        \Encore\Admin\Grid\Column::extend('tooltip', \App\Extensions\Column\Tooltip::class);

        \Encore\Admin\Grid::init(function (\Encore\Admin\Grid $grid) {
            \Encore\Admin\Facades\Admin::script("$.fn.modal.Constructor.prototype.enforceFocus = function() {};");
            $grid->actions(function (\Encore\Admin\Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });
            $grid->paginate(config('app.grid_paginate', 20));
            cache_grid_column($grid);
        });
    }

    protected  function initForm()
    {
        \Encore\Admin\Form::extend('DateMultiple', \App\Extensions\Form\Field\DateMultiple::class);
        \Encore\Admin\Form::extend('mobile',\App\Extensions\Form\Field\Mobile::class);

        \Encore\Admin\Form::extend('belongsToMany',\App\Extensions\Form\Field\BelongsToMany::class);
        \Encore\Admin\Form::extend('belongsTo',\App\Extensions\Form\Field\BelongsTo::class);

        \Encore\Admin\Form::extend('multipleFile', \App\Extensions\Form\Field\MultipleFile::class);
        \Encore\Admin\Form::extend('file', \App\Extensions\Form\Field\File::class);
        \Encore\Admin\Form::extend('image', \App\Extensions\Form\Field\Image::class);
        \Encore\Admin\Form::extend('multipleImage', \App\Extensions\Form\Field\MultipleImage::class);

        \Encore\Admin\Form::init(function (\Encore\Admin\Form $form) {

            $form->disableEditingCheck();
            $form->disableCreatingCheck();
            //$form->disableCreatingCheck();
            $form->tools(function (\Encore\Admin\Form\Tools $tools) {
                $tools->disableDelete();
                $tools->disableView();
            });
            $form->footer(function (\Encore\Admin\Form\Footer $footer) {
                $footer->disableReset();
                $footer->disableViewCheck();
                $footer->disableCreatingCheck();
                $footer->disableEditingCheck();
            });
        });
    }

    protected function initShow()
    {
        \Encore\Admin\Show::init(function (\Encore\Admin\Show $show) {
            $show->panel()->tools(function (\Encore\Admin\Show\Tools $tools) {
                $tools->disableDelete();
                $tools->disableEdit();
            });
        });
    }

    protected function script()
    {
        $script =  <<<SCRIPT

   $(".grid-row-edit").html("&nbsp;编辑&nbsp;");
   $(".grid-row-delete").html("&nbsp;删除");
    $('form').attr('autocomplete','off');

SCRIPT;
        \Encore\Admin\Admin::script($script);
    }

    protected function style()
    {
        \Encore\Admin\Facades\Admin::css('/vendor/static/main.css?v=' . date('Ymd'));
    }


    protected function js()
    {
        \Encore\Admin\Facades\Admin::js('/vendor/laravel-admin-ext/hui/layer/2.4/layer.js');
        \Encore\Admin\Facades\Admin::js('/vendor/laravel-admin-ext/hui/layer/2.4/layer.modal.js');
    }


}
